package com.inspection.action;

import com.inspection.common.Action;
import com.inspection.model.Host;
import com.inspection.model.HostInfo;
import com.inspection.model.HostStatus;
import com.inspection.operator.XmlAnalysis;
import com.inspection.utils.AppConfig;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by zhujun on 2015-6-4.
 */
public class DefaultAction extends Action {

    /**
     * 页面初始化
     *
     * @param request
     * @param response
     */
    public void index(HttpServletRequest request,HttpServletResponse response){

        request.setAttribute("apps",AppConfig.cache.get("apps") == null?null:AppConfig.cache.get("apps").getValue());
        request.setAttribute("hoststatus",AppConfig.cache.get("hoststatus") ==null?null:AppConfig.cache.get("hoststatus").getValue());

        this.render("default/index",request,response);

    }

    /**
     *
     * 显示HOST信息
     *
     * @param request
     * @param response
     */
    public void hostInfo(HttpServletRequest request,HttpServletResponse response){

        Host host =  (Host)AppConfig.hosts.get(request.getParameter("host"));
        request.setAttribute("host",host);

        HostStatus hostStatus = (HostStatus) ((HashMap)AppConfig.cache.get("hoststatus").getValue()).get(host.getIp());
        request.setAttribute("hostinfo",hostStatus);

        this.render("default/hostinfo",request,response);
    }

    /**
     *
     * 显示图例
     *
     * @param request
     * @param response
     */
    public void showimage(HttpServletRequest request,HttpServletResponse response){
        String host = request.getParameter("host");
        String type = request.getParameter("type");
        XmlAnalysis.showImageForHost(host,type,response);
    }

    /**
     * 刷新HOST状态
     *
     * @param request
     * @param response
     */
    public void reloadHost(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String ip = request.getParameter("ip");
        Host host = (Host)AppConfig.hosts.get(ip);
        XmlAnalysis.getHostInfo(host.getIp(),host.getPort());
        response.getWriter().print("success");
    }

    /**
     * 刷新APP状态
     *
     * @param request
     * @param response
     */
    public void reloadApp(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String url = request.getParameter("url");
        XmlAnalysis.getHttpInfo(url);
        response.getWriter().print("success");
    }

}
